package com.cch.demo.sys.service.impl;

import com.cch.demo.sys.domain.Role;
import com.cch.demo.sys.mapper.RoleMapper;
import com.cch.demo.sys.service.RoleService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 华哥
 * @since 2020-03-08
 */
@Service
@DS("master")
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {


    @Override
    @DS("master")
    public void saveRolePermission(Integer rid, Integer[] ids) {
        this.getBaseMapper().removeRolePermissionByRid(rid);
        if(ids!=null&&ids.length>0){
            for (Integer pid:ids){
                this.getBaseMapper().saveRolePermission(rid,pid);
            }
        }
    }

    @Override
    @DS("master")
    public List<Integer> queryUserRoleIdsByUid(Integer id) {

        return  this.getBaseMapper().queryRoleUserByUid(id);
    }

    @Override
    @DS("master")
    public void saveRoleUser(Integer uid, Integer[] ids) {
        //删除原有的sys_role_user
        this.getBaseMapper().removeRoleUserByUid(uid);
        //判断ids是否为空
        if(ids!=null&&ids.length>0){
            for (Integer rid:ids){
                this.getBaseMapper().saveRoleUser(uid,rid);
            }
        }

    }

    @Override
    @DS("master")
    public boolean removeById(Serializable id) {
        //删除sys_Role_Permission之间联系
        this.getBaseMapper().removeRolePermissionByRid(id);
        //删除sys_Role_user之间联系
        this.getBaseMapper().removeRoleUserByRid(id);
        return super.removeById(id);
    }

    @Override
    @DS("master")
    public List<Integer> queryRolePermissionByRid(Integer rid){
        return this.getBaseMapper().queryRolePermissionByRid(rid);
    }
}
